import SensorLabel from './SensorLabel.js';
import { CSS2DObject, CSS2DRenderer } from 'three/examples/jsm/renderers/CSS2DRenderer';
import smoke_jpg from '@/assets/smoke.jpg';
import temperature_jpg from '@/assets/temperature.jpg';
import door_jpg from '@/assets/door.jpg';
function rotation(direction) {
  let ro = [0, 0, 0];
  switch (direction) {
    case '朝前':
      ro = [0.5 * Math.PI, 0, 0];
      break;
    case '朝后':
      ro = [0.5 * Math.PI, 0, 0];
      break;
    case '朝上':
      ro = [0, 0, 0];
      break;
    case '朝下':
      ro = [Math.PI, 0, 0];
      break;
    case '朝左':
      ro = [-0.5 * Math.PI, 0, 0];
      break;
    case '朝右':
      ro = [0.5 * Math.PI, 0, 0];
      break;
    default:
      break;
  }

  return ro;
}

function createSmokeMash(x, y, z, direction) {
  var texture_loader = new THREE.TextureLoader();
  var texture = texture_loader.load(smoke_jpg);
  var geometry1 = new THREE.CylinderGeometry(1, 1, 0.2, 64);
  var material1 = new THREE.MeshLambertMaterial({
    color: 0xffffff,
    map: texture,
  }); //材质对象Material
  var mesh1 = new THREE.Mesh(geometry1, material1); //
  mesh1.position.set(x, y, z);
  mesh1.rotation.set(0.5 * Math.PI, 0, 0);
  
  return mesh1;
}

function createTemperatureMash(x, y, z, direction) {
  var texture_loader = new THREE.TextureLoader();
  var texture = texture_loader.load(temperature_jpg);
  var geometry1 = new THREE.BoxGeometry(1, 0.2, 1);
  var material1 = new THREE.MeshLambertMaterial({
    color: 0xffffff,
    map: texture,
  }); //材质对象Material
  var mesh1 = new THREE.Mesh(geometry1, material1); //
  mesh1.position.set(x, y, z);
  mesh1.rotation.set(0.5 * Math.PI, 0, 0);
  return mesh1;
}

function createDoorMash(x, y, z, direction)
{
    var texture_loader = new THREE.TextureLoader();
    var texture = texture_loader.load(door_jpg);
    var geometry1 = new THREE.BoxGeometry(1, 0.2, 1);
    var material1 = new THREE.MeshLambertMaterial({
      color: 0xffffff,
      map: texture,
    }); //材质对象Material
    var mesh1 = new THREE.Mesh(geometry1, material1); //
    mesh1.position.set(x, y, z);
    mesh1.rotation.set(0.5 * Math.PI, 0, 0);
    // mesh1.on('click', function(ev) { console.log('mesh click')});
    return mesh1;
}
function createSensor(category, x, y, z, direction) {
  var mesh = null;
  switch (category) {
    case 1:
      mesh = createSmokeMash(x, y, z, direction);
      break;
    case 2:
      mesh = createTemperatureMash(x, y, z, direction);
      break;
    case 3:
      mesh = createDoorMash(x, y, z, direction);
      break;
    default:
      break;
  }

  return mesh;
}

class Sensor {

  constructor(id,category, x, y, z, direction) {
    this._id = id;
    this._mesh = createSensor(category, x, y, z, direction);
    this._sensor_label = new SensorLabel();
    this._label = new CSS2DObject(this._sensor_label._div);
    this._label.position.set(0, 0, 0);
    this._label.visible = true;
    this._mesh.add(this._label);
    
    // this._mesh.cursor = 'pointer';
    // this._mesh.on('click', function(ev) { console.log('mesh click')});
    // this._mesh.addEventListener('mousedown',()=>{
    //     console.log('mesh click')
    //     this._label.visible = true;
    // })
  }
}
export default Sensor;
